package com.lanou.tk.tourism.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.tk.tourism.R;
import com.lanou.tk.tourism.adapter.HotelDetailsAdapter;
import com.lanou.tk.tourism.entity.HotelDetails;
import com.lanou.tk.tourism.entity.HotelDetailsData;
import com.lanou.tk.tourism.entity.HotelDetailse;

import java.util.List;


/**
 * Created by dllo on 15/10/30.
 */

public class HotelDetailsActivity extends Activity implements View.OnClickListener {
    private RecyclerView activity_details_hotel_Rv;
    private HotelDetailsAdapter hotelDetailsAdapter;
    private RequestQueue queue;
    private HotelDetailse data;
    private List<HotelDetailsData> hotel;
    private Button details_hotel_back;
    private TextView details_hotel_Tv;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_details_hotel);


        activity_details_hotel_Rv = (RecyclerView) findViewById(R.id.activity_details_hotel_Rv);
        hotelDetailsAdapter = new HotelDetailsAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        activity_details_hotel_Rv.setLayoutManager(gridLayoutManager);
        activity_details_hotel_Rv.setAdapter(hotelDetailsAdapter);

        details_hotel_Tv = (TextView) findViewById(R.id.details_hotel_Tv);
        details_hotel_back = (Button) findViewById(R.id.details_hotel_back);
        details_hotel_back.setOnClickListener(this);


        initData();

    }

    private void initData() {
        queue = Volley.newRequestQueue(this);

        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        String title = intent.getStringExtra("title");
        details_hotel_Tv.setText(title);
        String url = "http://open.qyer.com/qyer/hotel/search_list?checkin=2015-10-26%2000%3A00%3A01&checkout=2015-10-27%2023%3A59%3A59&city_id=" + id + "&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&from_key=app_ios_index_top&lat=38.88268166273701&lon=121.5393305953762&orderby=1&page=1&track_app_channel=App%2520Store&track_app_version=6.6.6&track_device_info=iPhone7%2C2&track_deviceid=7C4F45AC-D834-40CC-8E57-1C73070C0250&track_os=ios%25209.0.1&v=1";
        final StringRequest request = new StringRequest(url, new Response.Listener<String>() {


            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();

                HotelDetails hotelDetails = gson.fromJson(response, HotelDetails.class);

                data = hotelDetails.getData();
                hotel = data.getHotel();

                Log.i("HotelDetailsActivity", "------------------------------------------" + hotel);
                hotelDetailsAdapter.addDatas(hotel);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        queue.add(request);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.details_hotel_back:
                Intent intent = new Intent(this,RecommendHotelActivity.class);
                startActivity(intent);
                finish();

                break;

        }

    }
}
